// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2024-07-23

package commands

import (
	"fmt"
	"gitee.com/go-wares/framework-iris/framework/src/conf"
	"path/filepath"
	"strings"
)

// Common
// is a common method for framework starter and stopper.
type Common struct {
}

// PidFile
// returns a file name for pid file.
//
//	return "example.pid"
func (o *Common) PidFile() string {
	return fmt.Sprintf(`%s.pid`, conf.Config.Name)
}

// PidPath
// returns a path for pid file location.
//
//	return "/data/sketch"
func (o *Common) PidPath() string {
	if p, err := filepath.Abs("."); err == nil {
		return p
	}
	return "."
}

func (o *Common) WorkingDir(s string) string {
	if s == "" {
		s = "./"
	}
	if str, err := filepath.Abs(s); err == nil {
		return str
	}
	return strings.TrimSuffix(s, "/")
}
